package com.lib.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.lib.common.R;

/**
 * @author 一叶丶枫凌
 * Created on 2016-7-20 10:36
 * 自动旋转的图片控件
 */
public class AutoRotationLayout extends LinearLayout {
    public static final int DIRECTION_X = 0x0;
    public static final int DIRECTION_Y = 0x1;
    public static final int DIRECTION_Z = 0x2;
    public static final int DIRECTION_XYZ = 0x3;

    private int mRotateIntervalMillis = 100;
    private boolean mAutoRotate = true;
    private int mRotateTimes = 999999999;
    private int mRotateDirection = DIRECTION_Y;
    private int mRotatedAngle = 10;
    private int mHaveRotatedAngle;
    private int mHaveRotatedTimes;

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            mHaveRotatedAngle = (mHaveRotatedAngle + mRotatedAngle) % 360;
            if (mHaveRotatedAngle == 0) {
                mHaveRotatedTimes++;
            }
            if (mHaveRotatedTimes < mRotateTimes) {
                switch (mRotateDirection) {
                    case DIRECTION_X:
                        setRotationX(mHaveRotatedAngle);
                        break;
                    case DIRECTION_Y:
                        setRotationY(mHaveRotatedAngle);
                        break;
                    case DIRECTION_Z:
                        setRotation(mHaveRotatedAngle);
                        break;
                    case DIRECTION_XYZ:
                        // restore();
                        setRotationX(mHaveRotatedAngle);
                        setRotationY(mHaveRotatedAngle);
                        setRotation(mHaveRotatedAngle);
                }
                startRotate();
            }
        }
    };

    public AutoRotationLayout(Context context) {

        this(context, null);
    }

    public AutoRotationLayout(Context context, AttributeSet attrs) {

        this(context, attrs, 0);
    }

    public AutoRotationLayout(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoRotationLayout);
        mAutoRotate = a.getBoolean(R.styleable.AutoRotationLayout_auto_rotate, mAutoRotate);
        mRotateDirection = a.getInt(R.styleable.AutoRotationLayout_rotate_direction, mRotateDirection);
        mRotateIntervalMillis = a.getInt(R.styleable.AutoRotationLayout_rotate_intervalMillis, mRotateIntervalMillis);
        mRotateTimes = a.getInt(R.styleable.AutoRotationLayout_rotate_times, mRotateTimes);
        mRotatedAngle = a.getInt(R.styleable.AutoRotationLayout_rotate_angle, mRotatedAngle);
        a.recycle();
    }

    public void setRotateDirection(int rotateDirection) {

        mRotateDirection = rotateDirection;
    }

    public boolean isAutoRotate() {

        return mAutoRotate;
    }

    public int getRotateIntervalMillis() {

        return mRotateIntervalMillis;
    }

    // @IntDef({SPEED_FAST, INTERVAL_NORMAL, INTERVAL_SLOW})
    // @Retention(RetentionPolicy.SOURCE)
    // public @interface RotateSpeed {
    //
    // }

    /**
     * 旋转间隔，间隔越小，旋转越快
     *
     * @param millis 间隔时间，单位毫秒
     */
    public void setRotateIntervalMillis(int millis) {

        if (millis < 0) {
            millis = 0;
        }
        mRotateIntervalMillis = millis;
    }

    public void setAutoRotate(boolean autoRotate) {

        if (mAutoRotate != autoRotate) {
            mAutoRotate = autoRotate;
            if (mAutoRotate) {
                startRotate();
            } else {
                stopRotate();
            }
        }
    }

    private void restore() {

        setRotationX(0);
        setRotationY(0);
        setRotation(0);
    }

    @Override
    protected void onAttachedToWindow() {

        super.onAttachedToWindow();
        if (mAutoRotate) {
            startRotate();
        }
    }

    private void startRotate() {

        postDelayed(mRunnable, mRotateIntervalMillis);
    }

    @Override
    protected void onDetachedFromWindow() {

        super.onDetachedFromWindow();
        setAutoRotate(false);
    }

    private void stopRotate() {

        restore();
    }
}